package com.mingle.iwalk;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class StatusButton extends FrameLayout {

	private View mIcon;
	private ProgressBar mProgressBar;
	private TextView mInfo;
	private View view;
	
	private boolean mbEnable;
	
	public StatusButton(Context context) {
		this(context, null);
		
	}

	public StatusButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mbEnable = true;
		LayoutInflater inflater = LayoutInflater.from(context);

	    view = inflater.inflate(R.layout.status_button, this);
	    mIcon = view.findViewById(R.id.icon);
	    mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
	    mInfo = (TextView) view.findViewById(R.id.info); 
	    
	    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IWalkWidget, 0, 0);
	    Drawable bg = a.getDrawable(R.styleable.IWalkWidget_background);
	    Drawable icon = a.getDrawable(R.styleable.IWalkWidget_icon);
	    String lab = a.getString(R.styleable.IWalkWidget_text);
	    a.recycle();
	    
	    if(bg != null)
	    {
	    	view.setBackgroundDrawable(bg);
	    }
	    
	    if(icon != null)
	    {
	    	mIcon.setBackgroundDrawable(icon);
	    }
	    
	    if(lab!=null && lab.length()!=0)
	    {
	    	mInfo.setText(lab);
	    }
	}
	
	public void setText(int resId)
	{
		if(mbEnable)
		{
			mInfo.setText(resId);
		}
	}
	
	public void showIcon()
	{
		mIcon.setVisibility(View.VISIBLE);
		mProgressBar.setVisibility(View.INVISIBLE);
	}
	
	public void showProgressBar()
	{
		mIcon.setVisibility(View.INVISIBLE);
		mProgressBar.setVisibility(View.VISIBLE);
	}

	@Override
	public void setEnabled(boolean enabled) {
		mbEnable = enabled;
		view.setFocusable(enabled);
		mInfo.setTextColor(enabled?Color.WHITE:Color.GRAY);
		mInfo.setEnabled(enabled);
		super.setEnabled(enabled);
	}
	
	
}
